home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 17ICM86 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  9.6 KB  |  324 lines

  1. package com.sun.java.swing;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.Rectangle;
  9. import java.awt.Shape;
  10. import java.awt.image.ImageObserver;
  11. import java.util.Vector;
  12.  
  13. class SwingGraphics extends Graphics {
  14.    Graphics graphics;
  15.    Rectangle clipRect;
  16.    static final int poolSize = 20;
  17.    private static int needsSetClip = -1;
  18.    private static final Object graphicsPoolKey;
  19.    static Class class$com$sun$java$swing$SwingGraphics;
  20.  
  21.    static {
  22.       Class var10000 = class$com$sun$java$swing$SwingGraphics;
  23.       if (var10000 == null) {
  24.          try {
  25.             var10000 = Class.forName("com.sun.java.swing.SwingGraphics");
  26.          } catch (ClassNotFoundException var0) {
  27.             throw new NoClassDefFoundError(((Throwable)var0).getMessage());
  28.          }
  29.  
  30.          class$com$sun$java$swing$SwingGraphics = var10000;
  31.       }
  32.  
  33.       graphicsPoolKey = var10000;
  34.    }
  35.  
  36.    SwingGraphics(Graphics g) {
  37.       this.init(g);
  38.    }
  39.  
  40.    private void _changeClip(int x, int y, int w, int h, boolean set) {
  41.       if (set) {
  42.          this.clipRect.x = x;
  43.          this.clipRect.y = y;
  44.          this.clipRect.width = w;
  45.          this.clipRect.height = h;
  46.       } else {
  47.          SwingUtilities.computeIntersection(x, y, w, h, this.clipRect);
  48.       }
  49.  
  50.    }
  51.  
  52.    public void clearRect(int x, int y, int width, int height) {
  53.       this.graphics.clearRect(x, y, width, height);
  54.    }
  55.  
  56.    public void clipRect(int x, int y, int width, int height) {
  57.       this.graphics.clipRect(x, y, width, height);
  58.       this._changeClip(x, y, width, height, false);
  59.    }
  60.  
  61.    public void copyArea(int x, int y, int width, int height, int dx, int dy) {
  62.       this.graphics.copyArea(x, y, width, height, dx, dy);
  63.    }
  64.  
  65.    public Graphics create() {
  66.       return createSwingGraphics(this.graphics.create());
  67.    }
  68.  
  69.    public Graphics create(int x, int y, int w, int h) {
  70.       return createSwingGraphics(createGraphics(this.graphics, x, y, w, h));
  71.    }
  72.  
  73.    static Graphics createGraphics(Graphics g, int x, int y, int width, int height) {
  74.       Graphics cg = g.create(x, y, width, height);
  75.       if (needsSetClip != 0) {
  76.          Rectangle r = g.getClipBounds();
  77.          int X = r.x > x ? r.x - x : 0;
  78.          int Y = r.y > y ? r.y - y : 0;
  79.          int W = Math.min(x + width, r.x + r.width) - Math.max(x, r.x);
  80.          int H = Math.min(y + height, r.y + r.height) - Math.max(y, r.y);
  81.          if (needsSetClip == -1 && (x > 0 || y > 0)) {
  82.             Rectangle r2 = cg.getClipBounds();
  83.             if (r2.x == X && r2.y == Y && r2.width == W && r2.height == H) {
  84.                needsSetClip = 0;
  85.             } else {
  86.                needsSetClip = 1;
  87.             }
  88.          }
  89.  
  90.          cg.setClip(X, Y, W, H);
  91.       }
  92.  
  93.       return cg;
  94.    }
  95.  
  96.    public static SwingGraphics createSwingGraphics(Graphics g) {
  97.       if (g == null) {
  98.          return null;
  99.       } else if (g instanceof SwingGraphics) {
  100.          return (SwingGraphics)g;
  101.       } else {
  102.          SwingGraphics sg = getRecycledSwingGraphics();
  103.          if (sg != null) {
  104.             sg.init(g);
  105.          } else {
  106.             sg = new SwingGraphics(g);
  107.          }
  108.  
  109.          return sg;
  110.       }
  111.    }
  112.  
  113.    public void dispose() {
  114.       if (this.graphics != null) {
  115.          this.graphics.dispose();
  116.          this.graphics = null;
  117.       }
  118.  
  119.       this.recycle();
  120.    }
  121.  
  122.    public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
  123.       this.graphics.drawArc(x, y, width, height, startAngle, arcAngle);
  124.    }
  125.  
  126.    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
  127.       return this.graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer);
  128.    }
  129.  
  130.    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
  131.       return this.graphics.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
  132.    }
  133.  
  134.    public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
  135.       return this.graphics.drawImage(img, x, y, width, height, bgcolor, observer);
  136.    }
  137.  
  138.    public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
  139.       return this.graphics.drawImage(img, x, y, width, height, observer);
  140.    }
  141.  
  142.    public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
  143.       return this.graphics.drawImage(img, x, y, bgcolor, observer);
  144.    }
  145.  
  146.    public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
  147.       return this.graphics.drawImage(img, x, y, observer);
  148.    }
  149.  
  150.    public void drawLine(int x1, int y1, int x2, int y2) {
  151.       this.graphics.drawLine(x1, y1, x2, y2);
  152.    }
  153.  
  154.    public void drawOval(int x, int y, int width, int height) {
  155.       this.graphics.drawOval(x, y, width, height);
  156.    }
  157.  
  158.    public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
  159.       this.graphics.drawPolygon(xPoints, yPoints, nPoints);
  160.    }
  161.  
  162.    public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
  163.       this.graphics.drawPolyline(xPoints, yPoints, nPoints);
  164.    }
  165.  
  166.    public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
  167.       this.graphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
  168.    }
  169.  
  170.    public void drawString(String str, int x, int y) {
  171.       this.graphics.drawString(str, x, y);
  172.    }
  173.  
  174.    public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
  175.       this.graphics.fillArc(x, y, width, height, startAngle, arcAngle);
  176.    }
  177.  
  178.    public void fillOval(int x, int y, int width, int height) {
  179.       this.graphics.fillOval(x, y, width, height);
  180.    }
  181.  
  182.    public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
  183.       this.graphics.fillPolygon(xPoints, yPoints, nPoints);
  184.    }
  185.  
  186.    public void fillRect(int x, int y, int width, int height) {
  187.       this.graphics.fillRect(x, y, width, height);
  188.    }
  189.  
  190.    public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
  191.       this.graphics.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
  192.    }
  193.  
  194.    public Shape getClip() {
  195.       return this.graphics.getClip();
  196.    }
  197.  
  198.    public Rectangle getClipBounds() {
  199.       return this.graphics.getClipBounds();
  200.    }
  201.  
  202.    public int getClipHeight() {
  203.       return this.clipRect.height;
  204.    }
  205.  
  206.    public int getClipWidth() {
  207.       return this.clipRect.width;
  208.    }
  209.  
  210.    public int getClipX() {
  211.       return this.clipRect.x;
  212.    }
  213.  
  214.    public int getClipY() {
  215.       return this.clipRect.y;
  216.    }
  217.  
  218.    public Color getColor() {
  219.       return this.graphics.getColor();
  220.    }
  221.  
  222.    public Font getFont() {
  223.       return this.graphics.getFont();
  224.    }
  225.  
  226.    public FontMetrics getFontMetrics(Font f) {
  227.       return this.graphics.getFontMetrics(f);
  228.    }
  229.  
  230.    private static synchronized SwingGraphics getRecycledSwingGraphics() {
  231.       SwingGraphics r = null;
  232.       Vector pool = (Vector)SwingUtilities.appContextGet(graphicsPoolKey);
  233.       if (pool == null) {
  234.          return null;
  235.       } else {
  236.          int size;
  237.          if ((size = pool.size()) > 0) {
  238.             r = (SwingGraphics)pool.elementAt(size - 1);
  239.             pool.removeElementAt(size - 1);
  240.          }
  241.  
  242.          return r;
  243.       }
  244.    }
  245.  
  246.    void init(Graphics g) {
  247.       if (g == null) {
  248.          Thread.currentThread();
  249.          Thread.dumpStack();
  250.       }
  251.  
  252.       this.graphics = g;
  253.       this.clipRect = g.getClipBounds();
  254.       if (this.clipRect == null) {
  255.          this.clipRect = new Rectangle(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
  256.       }
  257.  
  258.    }
  259.  
  260.    public boolean isClipIntersecting(Rectangle r) {
  261.       if (this.clipRect.x < r.x + r.width && this.clipRect.x + this.clipRect.width > r.x && this.clipRect.y < r.y + r.height && this.clipRect.y + this.clipRect.height > r.y) {
  262.          return this.clipRect.width != 0 && this.clipRect.height != 0 && r.width != 0 && r.height != 0;
  263.       } else {
  264.          return false;
  265.       }
  266.    }
  267.  
  268.    public void recycle() {
  269.       this.graphics = null;
  270.       recycleSwingGraphics(this);
  271.    }
  272.  
  273.    private static synchronized void recycleSwingGraphics(SwingGraphics g) {
  274.       Vector pool = (Vector)SwingUtilities.appContextGet(graphicsPoolKey);
  275.       if (pool == null) {
  276.          pool = new Vector(20);
  277.          SwingUtilities.appContextPut(graphicsPoolKey, pool);
  278.       }
  279.  
  280.       if (pool.size() < 20) {
  281.          pool.addElement(g);
  282.       }
  283.  
  284.    }
  285.  
  286.    public void setClip(int x, int y, int width, int height) {
  287.       this.graphics.setClip(x, y, width, height);
  288.       this._changeClip(x, y, width, height, true);
  289.    }
  290.  
  291.    public void setClip(Shape clip) {
  292.       this.graphics.setClip(clip);
  293.       if (clip instanceof Rectangle) {
  294.          Rectangle r = (Rectangle)clip;
  295.          this._changeClip(r.x, r.y, r.width, r.height, true);
  296.       }
  297.  
  298.    }
  299.  
  300.    public void setColor(Color c) {
  301.       this.graphics.setColor(c);
  302.    }
  303.  
  304.    public void setFont(Font font) {
  305.       this.graphics.setFont(font);
  306.    }
  307.  
  308.    public void setPaintMode() {
  309.       this.graphics.setPaintMode();
  310.    }
  311.  
  312.    public void setXORMode(Color c1) {
  313.       this.graphics.setXORMode(c1);
  314.    }
  315.  
  316.    public Graphics subGraphics() {
  317.       return this.graphics;
  318.    }
  319.  
  320.    public void translate(int x, int y) {
  321.       this.graphics.translate(x, y);
  322.    }
  323. }
  324.